import 'dart:math';

import 'package:flutter/material.dart';

class GridViewWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GridView.builder(
      itemCount: 100,
        gridDelegate:
            SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 3),
        itemBuilder: (BuildContext bctx, int index) {
          return Container(
            //自定义高度不生效
//            height: 200,
              color: Color.fromARGB(255, Random().nextInt(255),
                  Random().nextInt(255), Random().nextInt(255)));
        });
  }
}

class GirdViewDemo2Widget extends StatelessWidget {
  const GirdViewDemo2Widget({
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GridView(
      gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
        maxCrossAxisExtent: 100, //item占有的大概宽度值
        crossAxisSpacing: 8,
        mainAxisSpacing: 8,
      ),
      children: List.generate(100, (index) {
        return Container(
            color: Color.fromARGB(255, Random().nextInt(255),
                Random().nextInt(255), Random().nextInt(255)));
      }),
    );
  }
}

class GridViewDemo1Widget extends StatelessWidget {
  const GridViewDemo1Widget({
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(8.0),
      child: GridView(
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 3, //如果是垂直方向，表示几列
            childAspectRatio: .8, //item的宽/高比例，可以确定item高度
            mainAxisSpacing: 8, //主轴的间隙
            crossAxisSpacing: 8 //交叉轴的间隙
            ),
        children: List.generate(100, (index) {
          return Container(
            color: Color.fromARGB(255, Random().nextInt(255),
                Random().nextInt(255), Random().nextInt(255)),
          );
        }),
      ),
    );
  }
}
